﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.House.DAL
{
    /// <summary>
    /// 数据访问类ChangeLog。
    /// </summary>
    public class ChangeLog
    {
        public ChangeLog()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid RecordId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ChangeLog where RecordId=@RecordId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.House.Entity.ChangeLog model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ChangeLog(");
            strSql.Append("RecordId,HouseId,HouseCode,HouseAddress,ChangTime,Operater,OperaterCode,OperaterId,OprDepartId,OprDepart,OprCompanyId,OprCompany,OprCityId,OprCity,SourceState,TargetState,OprType)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@HouseId,@HouseCode,@HouseAddress,@ChangTime,@Operater,@OperaterCode,@OperaterId,@OprDepartId,@OprDepart,@OprCompanyId,@OprCompany,@OprCityId,@OprCity,@SourceState,@TargetState,@OprType)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "HouseId", DbType.Guid, model.HouseId);
            db.AddInParameter(dbCommand, "HouseCode", DbType.AnsiString, model.HouseCode);
            db.AddInParameter(dbCommand, "HouseAddress", DbType.AnsiString, model.HouseAddress);
            db.AddInParameter(dbCommand, "ChangTime", DbType.DateTime, model.ChangTime);
            db.AddInParameter(dbCommand, "Operater", DbType.AnsiString, model.Operater);
            db.AddInParameter(dbCommand, "OperaterCode", DbType.AnsiString, model.OperaterCode);
            db.AddInParameter(dbCommand, "OperaterId", DbType.Guid, model.OperaterId);
            db.AddInParameter(dbCommand, "OprDepartId", DbType.Guid, model.OprDepartId);
            db.AddInParameter(dbCommand, "OprDepart", DbType.AnsiString, model.OprDepart);
            db.AddInParameter(dbCommand, "OprCompanyId", DbType.Guid, model.OprCompanyId);
            db.AddInParameter(dbCommand, "OprCompany", DbType.AnsiString, model.OprCompany);
            db.AddInParameter(dbCommand, "OprCityId", DbType.Guid, model.OprCityId);
            db.AddInParameter(dbCommand, "OprCity", DbType.AnsiString, model.OprCity);
            db.AddInParameter(dbCommand, "SourceState", DbType.Int32, model.SourceState);
            db.AddInParameter(dbCommand, "TargetState", DbType.Int32, model.TargetState);
            db.AddInParameter(dbCommand, "OprType", DbType.AnsiString, model.OprType);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.House.Entity.ChangeLog model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ChangeLog set ");
            strSql.Append("HouseId=@HouseId,");
            strSql.Append("HouseCode=@HouseCode,");
            strSql.Append("HouseAddress=@HouseAddress,");
            strSql.Append("ChangTime=@ChangTime,");
            strSql.Append("Operater=@Operater,");
            strSql.Append("OperaterCode=@OperaterCode,");
            strSql.Append("OperaterId=@OperaterId,");
            strSql.Append("OprDepartId=@OprDepartId,");
            strSql.Append("OprDepart=@OprDepart,");
            strSql.Append("OprCompanyId=@OprCompanyId,");
            strSql.Append("OprCompany=@OprCompany,");
            strSql.Append("OprCityId=@OprCityId,");
            strSql.Append("OprCity=@OprCity,");
            strSql.Append("SourceState=@SourceState,");
            strSql.Append("TargetState=@TargetState,");
            strSql.Append("OprType=@OprType");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "HouseId", DbType.Guid, model.HouseId);
            db.AddInParameter(dbCommand, "HouseCode", DbType.AnsiString, model.HouseCode);
            db.AddInParameter(dbCommand, "HouseAddress", DbType.AnsiString, model.HouseAddress);
            db.AddInParameter(dbCommand, "ChangTime", DbType.DateTime, model.ChangTime);
            db.AddInParameter(dbCommand, "Operater", DbType.AnsiString, model.Operater);
            db.AddInParameter(dbCommand, "OperaterCode", DbType.AnsiString, model.OperaterCode);
            db.AddInParameter(dbCommand, "OperaterId", DbType.Guid, model.OperaterId);
            db.AddInParameter(dbCommand, "OprDepartId", DbType.Guid, model.OprDepartId);
            db.AddInParameter(dbCommand, "OprDepart", DbType.AnsiString, model.OprDepart);
            db.AddInParameter(dbCommand, "OprCompanyId", DbType.Guid, model.OprCompanyId);
            db.AddInParameter(dbCommand, "OprCompany", DbType.AnsiString, model.OprCompany);
            db.AddInParameter(dbCommand, "OprCityId", DbType.Guid, model.OprCityId);
            db.AddInParameter(dbCommand, "OprCity", DbType.AnsiString, model.OprCity);
            db.AddInParameter(dbCommand, "SourceState", DbType.Int32, model.SourceState);
            db.AddInParameter(dbCommand, "TargetState", DbType.Int32, model.TargetState);
            db.AddInParameter(dbCommand, "OprType", DbType.AnsiString, model.OprType);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ChangeLog ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.House.Entity.ChangeLog GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,HouseId,HouseCode,HouseAddress,ChangTime,Operater,OperaterCode,OperaterId,OprDepartId,OprDepart,OprCompanyId,OprCompany,OprCityId,OprCity,SourceState,TargetState,OprType from ChangeLog ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Chenxin.Lib.House.Entity.ChangeLog model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,HouseId,HouseCode,HouseAddress,ChangTime,Operater,OperaterCode,OperaterId,OprDepartId,OprDepart,OprCompanyId,OprCompany,OprCityId,OprCity,SourceState,TargetState,OprType ");
            strSql.Append(" FROM ChangeLog ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageIndex, int PageSize, String OrderField, string TableName, string StrWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");



            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);

            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, StrWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.House.Entity.ChangeLog> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,HouseId,HouseCode,HouseAddress,ChangTime,Operater,OperaterCode,OperaterId,OprDepartId,OprDepart,OprCompanyId,OprCompany,OprCityId,OprCity,SourceState,TargetState,OprType ");
            strSql.Append(" FROM ChangeLog ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.House.Entity.ChangeLog> list = new List<Chenxin.Lib.House.Entity.ChangeLog>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.House.Entity.ChangeLog ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.House.Entity.ChangeLog model = new Chenxin.Lib.House.Entity.ChangeLog();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            ojb = dataReader["HouseId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.HouseId = new Guid(ojb.ToString());
            }
            model.HouseCode = dataReader["HouseCode"].ToString();
            model.HouseAddress = dataReader["HouseAddress"].ToString();
            ojb = dataReader["ChangTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ChangTime = (DateTime)ojb;
            }
            model.Operater = dataReader["Operater"].ToString();
            model.OperaterCode = dataReader["OperaterCode"].ToString();
            ojb = dataReader["OperaterId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.OperaterId = new Guid(ojb.ToString());
            }
            ojb = dataReader["OprDepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.OprDepartId = new Guid(ojb.ToString());
            }
            model.OprDepart = dataReader["OprDepart"].ToString();
            ojb = dataReader["OprCompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.OprCompanyId = new Guid(ojb.ToString());
            }
            model.OprCompany = dataReader["OprCompany"].ToString();
            ojb = dataReader["OprCityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.OprCityId = new Guid(ojb.ToString());
            }
            model.OprCity = dataReader["OprCity"].ToString();
            ojb = dataReader["SourceState"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.SourceState = (int)ojb;
            }
            ojb = dataReader["TargetState"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.TargetState = (int)ojb;
            }
            model.OprType = dataReader["OprType"].ToString();
            return model;
        }

        #endregion  成员方法
    }
}

